//
//  CAIManager.cpp
//  Monster_Martial_Arts_Version_0
//
//  Created by chiawei.tseng on 13/1/15.
//
//

#include "CAIManager.h"
#include "cocos2d.h"
#include "CBaseAI.h"
#include "BattleDef.h"
//#include "CAIBehaviorWaitCommand.h"

using namespace MMA;

CAIManager::CAIManager()
{
    m_MyMaster = NULL;
}

CAIManager::~CAIManager()
{
    
}

void CAIManager::Init(CBattleObject* object)
{
    CCAssert(object, "AiManager's Master can not null");
    
    m_MyMaster = object ;
    
    IBaseState* currentState = m_mapState[m_CurrentStateID];
    
    currentState->Entering(NULL);
}

void CAIManager::Update(IStateInput* input)
{
    if (m_CurrentStateID == BATTLE_OBJECT_STATE_NONE)
        return;
        
    IBaseState* currentState = m_mapState[m_CurrentStateID];
    
    int newStateID = currentState->ShouldChange(input);
    
    if (newStateID != BATTLE_OBJECT_STATE_NONE)
    {
        IBaseState* lastState = currentState;
        currentState = m_mapState[newStateID];
        
        m_PreCurrentStateID = m_CurrentStateID;
        m_CurrentStateID = newStateID;
        
        lastState->Leaving(input);
        currentState->Entering(input);
    }
    
    currentState->Update(input);
}

void CAIManager::Release()
{
    RemoveAll();
}

void CAIManager::JumpToState(const int nextSateID , IStateInput* input)
{
    IBaseState* lastState = NULL;
    if (m_CurrentStateID != BATTLE_OBJECT_STATE_NONE)
    {
        lastState = m_mapState[m_CurrentStateID];
    }
    
    IBaseState* currentState = NULL;
    if (nextSateID != BATTLE_OBJECT_STATE_NONE)
    {
        currentState = m_mapState[nextSateID];
    }
    
    m_PreCurrentStateID = m_CurrentStateID;
    
    m_CurrentStateID = nextSateID;
    
    if (lastState) {
        lastState->Leaving(input);
    }
    
    currentState->Entering(input);
    
    currentState->Update(input);
    

}

